package com.jumi.microservice.yzhpay.pay.upload;

import com.jumi.microservice.yzhpay.constant.CardTypeEnum;
import com.jumi.microservice.yzhpay.constant.ConfigPath;
import com.jumi.microservice.yzhpay.dto.request.UploadExemptedInfoListRequest;
import com.jumi.microservice.yzhpay.pay.BasePay;
import com.jumi.microservice.yzhpay.util.Property;
import org.apache.commons.codec.binary.Base64;
//import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class UploadExemptedInfoList extends BasePay<UploadExemptedInfoListRequest> {
    @Override
    public UploadExemptedInfoListRequest assembleParam() {
        return UploadExemptedInfoListRequest.builder()
                .id_card("372922199602014500")
                .card_type(CardTypeEnum.PASSPORT.getValue())
                .real_name("董世南")
                .comment_apply("一条记录")
                .broker_id(Property.getProperties(ConfigPath.YZH_BROKERID))
                .dealer_id(Property.getProperties(ConfigPath.YZH_DEALERID))
                .user_images(readImages())
                .country("CHN")
                .birthday("19960201")
                .gender("女")
                .notify_url(Property.getProperties(ConfigPath.YZH_BACKNOTIFY_URL))
                .ref("")
                .build();
    }

    private String[] readImages() {
        String imagePath = "/resources/image/images.png";
        //将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imagePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        //对字节数组Base64编码
//        BASE64Encoder encoder = new BASE64Encoder();
//        return new String[]{encoder.encode(data)};//返回Base64编码过的字节数组字符串
        //对字节数组Base64编码,返回Base64编码过的字节数组字符串
        return new String[]{Base64.encodeBase64String(data)};
    }
}
